from selenium import webdriver
import time, datetime


class Spider(object):
    def __init__(self):
        self.Chormdriver = webdriver.Chrome()
        self.url = "https://www.huya.com/l"
        self.time = datetime.datetime.now()
        self.page_num = 1
    def __del__(self):
        self.Chormdriver.close()
    def open_Chormdriver(self):
        self.Chormdriver.get(self.url)

    def seize_data(self):
        data = self.Chormdriver.find_elements_by_xpath("""//*[@id="js-live-list"]/li""")
        next_page = self.Chormdriver.find_elements_by_xpath("""//*[@class="laypage_next"]""")
        return data, next_page

    def page_turning(self, next_page):
        next_page[0].click()
        self.page_num+=1
        time.sleep(1)


    def cache_data(self, data):
        pass

    def save_data(self):
        pass


    def start(self):
        # 打开浏览器
        self.open_Chormdriver()
        while True:
            # 进行数据抓取
            data, next_page = self.seize_data()
            # 进行数据缓存入redis
            self.cache_data(data)
            # 判断是否有下一页内容
            if next_page == []:
                print("没了")
                break
            # 进行翻页动作
            self.page_turning(next_page)
            # 异步将redis的数据存入mysql
            self.save_data()
            print("已抓取页面计数：{},当前任务已用时：{}".format(self.page_num, datetime.datetime.now()-self.time))
            pass

if __name__ == '__main__':
    spider = Spider()
    spider.start()